/* post-detail.js */
import bs_href from '../../../asset/js/g-mixins'

var app = getApp();
Page({
    data: {
        isPlayingMusic: false,
    },
    onLoad: function (option) {
        var postId = option.id;
        // this.data.currentPostId = postId;
        this.setData({
            currentPostId:postId
        });
        var $this = this;
        
        /* 获取页面数据 */
        wx.request({
            url: bs_href + 'posts_content/post_detail',
            method: 'GET',
            dataType: 'json',
            data: {
                id: postId
            },
            success: function (res) {
                var data = res.data;
                var local_database = data.local_database;
                // console.log(local_database);
                // 如果在onLoad方法中，不是异步的去执行一个数据绑定
                // 则不需要使用this.setData方法
                // 只需要对this.data赋值即可实现数据绑定

                $this.setData({
                    post_item_data: local_database
                });

                wx.setStorageSync('key', {
                    game: "风暴英雄",
                    developer: "暴风"
                });
                console.log(res);
            }
        });

        /* 利用缓存模拟收藏功能 */
        var postsCollected = wx.getStorageSync('posts_collected');
        if (postsCollected) {
            var postcollected = postsCollected[postId];
            this.setData({
                collected: postcollected
            })
        } else {
            var postsCollected = {};
            postsCollected[postId] = false;
            wx.setStorageSync('posts_collected', postsCollected);
        }
        // 当页面音乐播放为true 且 再次进入的页面与音乐为同一页面，该页面音乐播放的参数为true
        if (app.globalData.g_isPlayingMusic && app.globalData.g_currentMusicPostId === postId) {
            this.setData({
                isPlayingMusic: true
            })
        }
        this.setMusicMonitor();
        console.log('g_isPlayingMusic:'+app.globalData.g_isPlayingMusic);
        console.log('isPlayingMusic:'+this.data.isPlayingMusic);
        console.log('g_currentMusicPostId:'+app.globalData.g_currentMusicPostId);
        console.log('postId:'+postId);
    },
    /* 监听音乐启动 */
    setMusicMonitor: function () {
        var $this = this;
        wx.onBackgroundAudioPlay(function () {
            $this.setData({
                isPlayingMusic: true
            })
            app.globalData.g_isPlayingMusic = true;
            app.globalData.g_currentMusicPostId = $this.data.currentPostId;
        });
        wx.onBackgroundAudioPause(function () {
            $this.setData({
                isPlayingMusic: false
            })
            app.globalData.g_isPlayingMusic = false;
            app.globalData.g_currentMusicPostId = null;
        });
        wx.onBackgroundAudioStop(function () {
            $this.setData({
                isPlayingMusic: false
            })
            app.globalData.g_isPlayingMusic = false;
            app.globalData.g_currentMusicPostId = null;
        });
    },
    /* onCollectionTap:function(event){
        var game=wx.getStorageSync('key');
        console.log(game);
    },
    onShareTap:function(event){
        wx.removeStorageSync('key');
        // wx.clearStorageSync();//清除所有缓存，不需要填参数
        // 缓存的上限最大不能超过10MB
    } */
    /* 点击收藏图标，获取现有的收藏状态，反转状态，替换当前状态 */
    onColletionTap: function (event) {
        this.getPostsCollectedSyc();
        // this.getPostsCollectedAsy();
    },
    getPostsCollectedAsy: function () {
        var $this = this;
        wx.getStorage({
            key: "posts_collected",
            success: function (res) {
                var postsCollected = res.data;
                var postCollected = postsCollected[$this.data.currentPostId];
                // 收藏状态变成相反状态
                postCollected = !postCollected;
                postsCollected[$this.data.currentPostId] = postCollected;
                $this.showToast(postsCollected, postCollected);
            }
        })
    },
    getPostsCollectedSyc: function () {
        var postsCollected = wx.getStorageSync('posts_collected');
        var postCollected = postsCollected[this.data.currentPostId];
        postCollected = !postCollected;
        postsCollected[this.data.currentPostId] = postCollected;

        this.showToast(postsCollected, postCollected);
    },
    /* showModal: function (postsCollected, postCollected) {
        var $this = this;
        wx.showModal({
            title: '收藏',
            content: postCollected ? '收藏该文章？' : "取消收藏该文章？",
            showCancel: 'true',
            cancelText: '取消',
            cancelColor: '#333',
            confirmText: '确认',
            confirmColor: '#405f80',
            success: function (res) {
                if (res.confirm) {
                    //更新文章缓存值
                    wx.setStorageSync('posts_collected', postsCollected);
                    $this.showToast(postsCollected, postCollected);
                    // 更新数据绑定变量，从而实现切换class
                    $this.setData({
                        collected: postCollected
                    });
                }
            }
        });
    }, */
    showToast: function (postsCollected, postCollected) {
        wx.setStorageSync('posts_collected', postsCollected);
        this.setData({
            collected: postCollected
        });
        wx.showToast({
            title: postCollected ? "收藏成功" : "取消成功",
            duration: 1000,
            icon: 'success'
        });
    },
    // 事件处理函数，加一个event
    onShareTap: function (event) {
        var itemList = [
            "分享给微信好友",
            "分享到朋友圈",
            "分享到QQ",
            "分享到微信"
        ];
        wx.showActionSheet({
            itemList: itemList,
            itemColor: "#405f80",
            success: function (res) {
                // 用户点击的按钮，从上到下的顺序，从0开始
                var tapIndex = res.tapIndex;
                wx.showModal({
                    title: "用户" + itemList[tapIndex],
                    content: "现在无法实现分享功能"
                });
            }
        });
    },
    /* 音乐播放 */
    onMusicTap: function (event) {
        var dataMusic = this.data.post_item_data.music;
        var isPlayingMusic = this.data.isPlayingMusic;
        if (isPlayingMusic) {
            wx.pauseBackgroundAudio();
            this.setData({
                isPlayingMusic: false,
            })
            app.globalData.g_isPlayingMusic = false;
        } else {
            wx.playBackgroundAudio({
                dataUrl: dataMusic.url,
                title: dataMusic.title,
                coverImgUrl: dataMusic.coverImg,
            })
            this.setData({
                isPlayingMusic: true,
            })
            app.globalData.g_currentMusicPostId = this.data.currentPostId;
            app.globalData.g_isPlayingMusic = true;
        }
        

    }
})